package ru.ssau.berlin.jsf.converters;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ru.ssau.berlin.ejb.facade.EventFacade;
import ru.ssau.berlin.ejb.facade.EventFacadeLocal;
import ru.ssau.berlin.entity.EventType;
import ru.ssau.berlin.framework.JNDIHelper;


public class EventTypeConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        InitialContext ic;
        EventType type = null;
        try {
            ic = new InitialContext();
            EventFacadeLocal facade =
                    (EventFacadeLocal) ic.lookup(JNDIHelper.getBeanName(EventFacade.class));
            type = facade.findEventType(Long.parseLong(value));
        } catch (NamingException ex) {
            Logger.getLogger(EventTypeConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return type;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        EventType type = (EventType) value;
        return type.getId().toString();
    }

}
